﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NSun;
using NBearLite;
using Runda.CMS.Order.Entity;
using Runda.CMS.Order.Repository; 

namespace OrderPortal.Controllers
{
    public class OrderController : Controller
    {
        private OrderRepository orderRepository = new OrderRepository();

        private ShippingRepository shippingRepository = new ShippingRepository();

        private CustomerRepository customerRepository = new CustomerRepository();

        private InventoryRepository inventoryRepository = new InventoryRepository();

        private int pagesize = 20;

        [RequiresAuthentication]
        public ActionResult List(int? id) { 
            ViewData["list"] = BindList(id);
            return View();
        }

        protected Webdiyer.WebControls.Mvc.PagedList<OrderInfo> BindList(int? id)
        {
            int countPage = 0;
            Account account = System.Web.HttpContext.Current.Session["currentUser"] as Account;
            String customerId = account.customerID;
            String inventoryName = "";
            DateTime? dateFrom = null;
            DateTime? dateTo = null;
            if (Request["inventoryName"] != null)
            {
                inventoryName = GetFormValue("inventoryName").ToString();
            }
            if (Request["dateFrom"] != null && Request["dateFrom"] != String.Empty) {
                dateFrom = System.Convert.ToDateTime(GetFormValue("dateFrom").ToString());
            }
            if (Request["dateFrom"] != null && Request["dateTo"] != String.Empty) {
                dateTo = System.Convert.ToDateTime(GetFormValue("dateTo").ToString());
            }
            List<OrderInfo> list = orderRepository.CustomerQuery(customerId, inventoryName,dateFrom,dateTo,pagesize, id ?? 1, out countPage);          
            Webdiyer.WebControls.Mvc.PagedList<OrderInfo> p = new Webdiyer.WebControls.Mvc.PagedList<OrderInfo>(list, id ?? 1, pagesize, countPage);             
            return p;
        }

        [HttpGet]
        [RequiresAuthentication]
        public ActionResult Info(String id) {
            List<CustomerOrder> orderDetails = orderRepository.findCustomerOrderDetailByOrderId(id);
            if (orderDetails != null && orderDetails.Count > 0) {
                ViewData["orderId"] = orderDetails[0].orderId;
                ViewData["memo"] = orderDetails[0].memo;
                ViewData["orderTime"] = orderDetails[0].newTime;
                ViewData["orderDate"] = orderDetails[0].date;
                //Customer customer = customerRepository.findCustomerByCustomerCode(orderDetails[0].customerId);
                //ViewData["customerName"] = customer.name;
                ViewData["customerName"] = orderDetails[0].CusName;
                List<OrderDetail> detais = new List<OrderDetail>();
                foreach (CustomerOrder order in orderDetails) {
                    OrderDetail detail = new OrderDetail();
                    //Inventory inventory = inventoryRepository.findInventoryBycInvCode(order.inventoryBatchCode);
                    //if (inventory != null)
                    //{
                    //    detail.quantity = order.quantity.ToString();
                    //    detail.unit = inventory.unit;
                    //    detail.inventoryName = inventory.name;
                    //    detais.Add(detail);
                    //}
                    detail.quantity = order.quantity.ToString();
                    detail.unit = order.invUnit;
                    detail.inventorystd = order.invStd;
                    detail.inventoryName = order.invName;
                    detais.Add(detail);
                    
                }
                ViewData["orderDetail"] = detais;
            }
            return View();
        }

        private object GetFormValue(string name)
        {
            ViewData[name] = Request[name];
            return ViewData[name];
        }
    }

    public class OrderDetail {
        public String inventoryName { get; set; }
        public String unit { get; set; }
        public String quantity { get; set; }
        public String inventorystd { get; set; }
        public String BatchCode { get; set; }
        public String VDate { get; set; }
    }
}
